<template>
    <section>

        <van-list
                v-model="loading"
                :finished="finished"
                finished-text="没有更多了"
                @load="onLoad"
        >
            <div class="facility-div" v-for="(item, index) in msg" :key="index" @click="getDetail(item.id)">
                <div class="facility-div-left">
                    <img class="facility-div-left-img" :src="item.picture"/>
                </div>
                <div class="facility-div-right">
                    <div class="facility-div-right-title">
                        <div>{{item.equipmentName}}</div>
                        <div class="switch" :class="{'switch-t':item.equipmentState}"  @click.stop="onInput(item,index)">
                            <div class="switch-con" :class="{'switch-con-t':item.equipmentState}"></div>
                        </div>

                    </div>
                    <div class="facility-div-right-con">型号：{{item.equipmentModel}}</div>
                    <div class="facility-div-right-con">功率：{{item.equipmentPower}} W</div>
                    <div class="facility-div-right-con">使用区域：{{item.fishTankName}}</div>
                    <div class="facility-div-right-con">是否作废：{{item.scrap}}</div>
                </div>
            </div>
        </van-list>
    </section>
</template>

<script>
import { getDate } from '../../../../api/unit.js'
import { updateEquipmentTask } from '../../../../api/index.js'
export default {
  props: {
    msg: Array,
    pageCount: Number,
    pageSize: Number
  },
  data () {
    return {
      loading: false,
      finished: false,
      checked: true
    }
  },
  filters: {
    format: function (value) {
      return getDate(value).nTime
    }
  },
  methods: {
    onLoad () {
      setTimeout(() => {
        if (this.msg.length < this.pageCount && this.pageCount > this.pageSize) {
          this.pageChange()
        }
        // 加载状态结束
        this.loading = false

        // 数据全部加载完成
        if (this.msg.length >= this.pageCount) {
          this.finished = true
        }
      }, 1000)
    },
    // showChange2 (index) {
    //   this.$emit('showChange', index)
    // },
    pageChange () {
      this.$emit('pageChange')
    },
    async onInput (checked, index) {
      let equipmentState = checked.equipmentState ? 0 : 1
      let data = {
        id: checked.id,
        equipmentState: equipmentState
      }
      await updateEquipmentTask(data)
      this.msg[index].equipmentState = !this.msg[index].equipmentState
      // debugger
      // this.$emit('ref')
    },
    getDetail (id) {
      this.$router.push({path: '/facility/detail', query: {id: id}})
    }
  }
}
</script>

<style scoped>
    .facility-div{
        margin-top: 22px;
        width:678px;
        height:209px;
        background:rgba(255,255,255,1);
        box-shadow:0px 1px 15px 0px rgba(194,194,194,0.77);
        border-radius:10px;
        display: flex;
    }
    .facility-div-left{
        width: 277px;
        height: 100%;
        background: #e4e4e4;
    }
    .facility-div-left-img{
        margin-left: 10px;
        width: 267px;
        height: 100%;
        height: 100%;
    }
    .facility-div-right{
        padding: 24px 19px 0px 29px;
    }
    .facility-div-right-title{
        display: flex;
        justify-content: space-between;
        width: 360px;
        font-size: 32px;
        margin-bottom: 8px;
    }
    .facility-div-right-con{
        font-size: 24px;
        color: #4d4d4d;
        margin-bottom: 10px;
    }
    .switch{
        width: 55px;
        height: 32px;
        background:#ABABAB;
        border-radius:16px;
        display: flex;
        align-items: center;
    }
    .switch-con{
        width:28px;
        height:28px;
        background:#fff;
        border-radius:50%;
        margin-left: 2px;
    }
    .switch-t{
        width: 55px;
        height: 32px;
        background:linear-gradient(35deg,rgba(0,186,255,1),rgba(11,155,238,1));
        border-radius:16px;
        display: flex;
        align-items: center;
    }
    .switch-con-t{
        width:28px;
        height:28px;
        background:#fff;
        border-radius:50%;
        margin-left: 25px;
    }
</style>
